package com.smiier.skin;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import cn.o.app.qrcode.CaptureView;
import cn.o.app.qrcode.CaptureView.CaptureListener;
import cn.skinapp.R;

import com.smiier.skin.constant.Constant;
import com.smiier.skin.ui.BasicActivity;

public class ScanActivity extends BasicActivity {

	protected CaptureView mCaptureView;
	boolean isDoctor;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_scan);
		init();
		setNavTitle("扫描二维码");
		isDoctor = getIntent().getBooleanExtra(Constant.IDENTITY_KEY_TITLE,
				false);
		mCaptureView = this.findViewById(R.id.capture_view, CaptureView.class);
		mCaptureView.setListener(new CaptureListener() {

			@Override
			public void onCapture(String data) {
				try {
					Intent intent = new Intent();
					if (!isDoctor) {
						if (data.startsWith("http://")) {
							JSONObject object = new JSONObject();
							String[] urlSplit = data.substring(
									data.indexOf("UrlControl/")
											+ "UrlControl/".length())
									.split("/");
							try {
								object.put("Uid", urlSplit[2]);
								object.put("User_type", urlSplit[1]);
								object.put("Method", urlSplit[0]);
							} catch (Exception e) {
								e.printStackTrace();
							}
							String content = object.toString();
							intent.putExtra(Constant.IDENTITY_KEY, content);
							setResult(0, intent);
						} else {
							intent.putExtra(Constant.IDENTITY_KEY,
									new JSONObject(data).toString());
							setResult(0, intent);
						}
					} else {
						intent.putExtra(Constant.IDENTITY_KEY, data);
						setResult(0, intent);
					}
					finish();
				} catch (Exception e) {

				}
			}
		});
	}

}